#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
@File Name  : logout.py
@Author     : LeeCQ
@Date-Time  : 2020/8/29 11:18
"""
from common import *
from login import login_easy
# login.py ->
from exc import ElementNotFind, KTMSError
import unittest


class TestLogout(unittest.TestCase):

    def setUp(self) -> None:
        self.driver = login_easy('test', '123456')

    def tearDown(self) -> None:
        self.driver.close()

    def test_logout(self):
        d = logout(self.driver)
        self.assertEqual(d.find_element_by_css_selector('#loginform > fieldset > legend').text.strip(),
                         '欢迎登陆K-TMS物流系统',
                         '登出成功')


def logout(_driver: BaseWebDriver):
    """登出

    :param _driver: 已登录的WebDriver对象
    :return: 0
    """
    try:
        _driver.find_element_by_xpath('//*[@id="topnav"]/li[4]').click()
        _driver.implicitly_wait(8)
        _driver.find_element_by_class_name('layui-layer-btn0').click()

    except ElementNotFind:
        logger.error('当前对象未登录')
        return _driver

    try:
        _driver.implicitly_wait(8)
        _driver.find_element_by_id('username')
        logger.info('登出成功')
        return _driver

    except ElementNotFind:
        print("登出是出现了点点小问题。")
        raise KTMSError("登出是出现了点点小问题。")


if __name__ == '__main__':
    from login import login_default

    logout(login_default())
